
library SpringPreloader initializer init requires SpringTable

	static if SPRING_PRELOADER then

	globals
		private integer KEY_SPRING_OBJECT_NAME_TO_ID
	    private constant integer DEFAULT_STRING = StringHash("Default string")
		private unit U
		private integer ENUM
	endglobals

	function GetObjectId takes string name returns integer 
		return LoadInteger(Spring_main_table, KEY_SPRING_OBJECT_NAME_TO_ID, StringHash(name))
	endfunction

	private function InjectAbilityId takes nothing returns boolean
        local integer id = ENUM
        local integer hash = StringHash(GetObjectName(id))
        if(hash != DEFAULT_STRING) then
            call UnitAddAbility(U,id)
            call UnitRemoveAbility(U,id)
            if(HaveSavedInteger(Spring_main_table, KEY_SPRING_OBJECT_NAME_TO_ID, hash)) then
                call BJDebugMsg("Warning: Ability name collision - " + GetObjectName(id))
            else
                call SaveInteger(Spring_main_table, KEY_SPRING_OBJECT_NAME_TO_ID, hash, id)
            endif 
        endif
        return false
    endfunction
	
	private function BatchInject takes integer a, integer b, integer c, integer d, integer e, integer f, integer g, integer h, integer i, integer j, integer k, integer l returns nothing
        local boolexpr E = Condition (function InjectAbilityId)
		local force F = SPRING_DUMMY_FORCE
		
		set ENUM = a
        call ForceEnumPlayersCounted(F, E, 1)
		set ENUM = b
        call ForceEnumPlayersCounted(F, E, 1)
		set ENUM = c
        call ForceEnumPlayersCounted(F, E, 1)
		set ENUM = d
        call ForceEnumPlayersCounted(F, E, 1)
		set ENUM = e
        call ForceEnumPlayersCounted(F, E, 1)
		set ENUM = f
        call ForceEnumPlayersCounted(F, E, 1)
		set ENUM = g
        call ForceEnumPlayersCounted(F, E, 1)
		set ENUM = h
        call ForceEnumPlayersCounted(F, E, 1)
		set ENUM = i
        call ForceEnumPlayersCounted(F, E, 1)
		set ENUM = j
        call ForceEnumPlayersCounted(F, E, 1)
		set ENUM = k
        call ForceEnumPlayersCounted(F, E, 1)
		set ENUM = l
        call ForceEnumPlayersCounted(F, E, 1)
		set E = null
		set F = null
    endfunction
	
	//! textmacro batch_inject_macro takes AID
    call BatchInject('$AID$0','$AID$1','$AID$2','$AID$3','$AID$4','$AID$5','$AID$6','$AID$7','$AID$8','$AID$9','$AID$A','$AID$B')
    call BatchInject('$AID$C','$AID$D','$AID$E','$AID$F','$AID$G','$AID$H','$AID$I','$AID$J','$AID$K','$AID$L','$AID$M','$AID$N')
    call BatchInject('$AID$O','$AID$P','$AID$Q','$AID$R','$AID$S','$AID$T','$AID$V','$AID$U','$AID$W','$AID$X','$AID$Y','$AID$Z')
    //! endtextmacro
	
	endif 
	
	private function init takes nothing returns nothing
		local unit u
		
		static if not SPRING_PRELOADER then
			return
		else

		set u = CreateUnit(Player(12),'hfoo',0.0, 0.0, 0)
		set U = u
		set KEY_SPRING_OBJECT_NAME_TO_ID = GetHandleId(u)
		//! runtextmacro batch_inject_macro("A00")
        //! runtextmacro batch_inject_macro("A01")
        //! runtextmacro batch_inject_macro("A02")
        //! runtextmacro batch_inject_macro("A03")
        //! runtextmacro batch_inject_macro("A04") 
        //! runtextmacro batch_inject_macro("A05")
        //! runtextmacro batch_inject_macro("A06")
        //! runtextmacro batch_inject_macro("A07")
        //! runtextmacro batch_inject_macro("A08")
        //! runtextmacro batch_inject_macro("A09")  
        //! runtextmacro batch_inject_macro("A0A")
        //! runtextmacro batch_inject_macro("A0B")
        //! runtextmacro batch_inject_macro("A0C")
        //! runtextmacro batch_inject_macro("A0D")
        //! runtextmacro batch_inject_macro("A0E") 
        //! runtextmacro batch_inject_macro("A0F")
        //! runtextmacro batch_inject_macro("A0G")
        //! runtextmacro batch_inject_macro("A0H")
        //! runtextmacro batch_inject_macro("A0I")
        //! runtextmacro batch_inject_macro("A0J") 
		call RemoveUnit(u)
		set u = null
		
		endif
	endfunction
	
endlibrary
